<template>
  <a-row style="padding: 10px;">
    <a-col span="24">
      <a-form layout="inline">
        <a-form-item label="管理员账号">
          <a-input v-model="search.account" placeholder="请输入管理员账号！"></a-input>
        </a-form-item>
        <a-form-item label="管理员电话">
          <a-input v-model="search.telephone" placeholder="请输入管理员电话！"></a-input>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="searchList">查询</a-button>
        </a-form-item>
      </a-form>
    </a-col>
    <a-col span="24" style="margin: 10px 0">
      <a-button type="primary" @click="addAdmin">新增管理员</a-button>
    </a-col>
    <a-col span="24">
      <a-table :dataSource="data" :columns="columns" :pagination="pagination" :loading="loading" size="small" bordered
               :rowKey="record => record.adminId">
        <template slot="operation" slot-scope="text, record, index">
          <div>
            <span>
              <a v-if="record.state == 1 || record.state == 0" @click="() => updateAdmin(index)">编辑</a>
              <a v-if="record.state == 1" @click="() => lockAdmin(index)">锁定</a>
              <a v-if="record.state == 1" @click="() => delAdmin(index)">删除</a>
              <a v-if="record.state == 2" @click="() => lockAdmin(index)">解锁</a>
              <a v-if="record.state == 1" @click="() => resetPwd(record)">重置密码</a>
            </span>
          </div>
        </template>
      </a-table>
      <NCPagination :page="pageObj" @changePage="(page)=>{this.pageObj = page}" @searchList="searchList"/>
    </a-col>
    <a-modal v-model="model.visible" :title="model.title">
      <a-form :form="model.form">
        <a-form-item label="管理员账号" :label-col="model.labelCol" :wrapper-col="model.wrapperCol">
          <a-input placeholder="请输入管理员账号！"
                   v-decorator="['account', {rules: [{ required: true, message: '请输入管理员账号！'}]}]"/>
        </a-form-item>
        <a-form-item label="管理员电话" :label-col="model.labelCol" :wrapper-col="model.wrapperCol">
          <a-input placeholder="请输入管理员电话！"
                   v-decorator="['telephone', {rules: [{ required: true, message: '请输入管理员电话！'}]}]"/>
        </a-form-item>
      </a-form>
      <template slot="footer">
        <a-button key="submit" type="primary" :loading="model.loading" @click="addOrUpdateAdminOk">
          确定
        </a-button>
        <a-button key="back" @click="model.visible = false">取消</a-button>
      </template>
    </a-modal>
  </a-row>
</template>

<script>
  import NCPagination from '../../components/NCPagination'

  export default {
    name: 'Admin',
    components: {
      NCPagination
    },
    data () {
      return {
        pageObj: {total: 10, pageSize: 10, pageNum: 1},
        search: {
          account: '',
          phone: ''
        },
        model: {
          labelCol: {span: 7},
          wrapperCol: {span: 12},
          loading: false,
          visible: false,
          title: '添加管理员',
          form: this.$form.createForm(this),
          data: {}
        },
        loading: false,
        data: [],
        pagination: false,
        columns: [
          {
            title: '序号',
            width: '20',
            customRender: (text, record, index) => {
              return index + this.pageObj.pageSize * (this.pageObj.pageNum - 1) + 1
            }
          }, {
            title: '管理员名称',
            dataIndex: 'account',
            sorter: true
          }, {
            title: '管理员电话',
            dataIndex: 'telephone',
            sorter: true
          }, {
            title: '创建时间',
            dataIndex: 'createTime',
            sorter: true
          }, {
            title: '状态',
            dataIndex: 'state',
            sorter: true,
            customRender: (text, record, index) => {
              return text === 2 ? '锁定' : '正常'
            }
          }, {
            title: '操作',
            fixed: 'right',
            width: 200,
            scopedSlots: {customRender: 'operation'}
          }
        ]
      }
    },
    mounted () {
      this.searchList()
    },
    methods: {
      // 查询数据
      searchList () {
        this.loading = true
        this.http.post('upmsApi', '/admin/admin/getAdminList', {
          pageNum: this.pageObj.pageNum,
          pageSize: this.pageObj.pageSize,
          data: {
            account: this.search.account,
            telephone: this.search.telephone
          }
        }).then((res) => {
          if (res.success) {
            this.data = res.rows
            this.pageObj.total = res.total
          }
          this.loading = false
        })
      },
      // 添加管理员
      addAdmin () {
        this.model.visible = true
        this.model.title = '添加管理员'
        this.model.data = {}
        this.model.form.setFieldsValue({
          account: null,
          telephone: null
        })
      },
      // 更新管理员
      updateAdmin (index) {
        this.model.visible = true
        this.model.title = '修改管理员'
        this.model.data = this.data[index]
        setTimeout(() => {
          this.model.form.setFieldsValue({
            account: this.model.data.account,
            telephone: this.model.data.telephone
          })
        }, 100)
      },
      // 删除管理员
      delAdmin (index) {
        const that = this
        this.$confirm({
          title: '删除管理员！?',
          onOk () {
            return new Promise((resolve, reject) => {
              console.log('删除')
              that.http.post('upmsApi', '/admin/admin/delAdmin', {adminId: that.data[index].adminId}).then((res) => {
                if (res.success) {
                  that.$message.info(res.message)
                  that.searchList()
                  resolve(res.message)
                } else {
                  reject(res.message)
                }
              })
            })
          }
        })
      },
      // 锁定/解锁管理员
      lockAdmin (index) {
        console.log('锁定管理员' + index)
        const that = this
        this.$confirm({
          title: that.data[index].state === 1 ? '锁定管理员！?' : '解锁管理员！',
          onOk () {
            return new Promise((resolve, reject) => {
              that.http.post('upmsApi', '/admin/admin/lockAdmin', {
                adminId: that.data[index].adminId,
                state: that.data[index].state === 1 ? 2 : 1
              }).then((res) => {
                if (res.success) {
                  that.$message.info(res.message)
                  that.searchList()
                  resolve(res.message)
                } else {
                  reject(res.message)
                }
              })
            })
          }
        })
      },
      // 添加或者更新管理员
      addOrUpdateAdminOk (e) {
        e.preventDefault()
        this.model.form.validateFields((err, values) => {
          if (!err) {
            console.log('Received values of form: ', values)
            this.model.loading = true
            let url = this.model.title === '添加管理员' ? '/admin/admin/addAdmin' : '/admin/admin/updateAdmin'
            this.http.post('upmsApi', url, {
              adminId: this.model.data.adminId,
              ...values
            }).then((res) => {
              if (res.success) {
                this.$message.info(res.message)
                this.searchList()
              }
              this.model.loading = false
              this.model.visible = false
            })
          }
        })
      },
      resetPwd (record) {
        console.log('重置管理员密码' + record)
        const that = this
        this.$confirm({
          title: '重置管理员密码? 重置后密码为123456',
          onOk () {
            return new Promise((resolve, reject) => {
              that.http.post('upmsApi', '/admin/admin/resetPwd', {
                adminId: record.adminId
              }).then((res) => {
                if (res.success) {
                  that.searchList()
                  resolve(res.message)
                } else {
                  reject(res.message)
                }
              })
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
